In this course, you're going to learn to write computer
programs in the Python programming language.
But before we get into that, we need to know exactly what programming is.
Fundamentally, programming is giving a list of instructions for
a computer to follow.
In the contest of programming,
this instructions are sometimes called algorithms.
Now computers are really good in following this instructions very reliably and
very quickly but not very creatively.
And we can't just give computers instructions in English or any other
natural language that we normally speak because natural language has ambiguity.
Words or sentences can have multiple meanings which computers can't figure out.
Instead we need to give computers instructions in a programming language,
which is a kind of language that computers can understand
because they have a formal syntax or set of rules.
There are many programming languages that exist including JavaScript, C++ Java,
Haskell and many many more.
But in this course we're going to learn to use the Python programming language.
The process of learning programming is more than just learning the rules of
the Python programming language.
It's also about how to breakdown and
solve problems regardless of the programming language.
And I like to think of programming as a translation process, where the programmer
translates their goals from natural language into a programming language.
For example,
if I want to write my own version of the game Wheel of Fortune, then I need to
translate the rules of the game into an unambiguous set of instructions written in
the Python programming language that tell the computer how to run the game.
And as you become a programmer, you'll learn how to come up with strategies, or
algorithms, for solving problems in how to translate these strategies
in the Python code that a computer can execute.
Now, all that said, the best way to learn programming is through practice, so
let's get started.
In this course, you'll be able to write Python codes right in your browser.
You'll see what are called active code windows
that look like this in your textbook.
Now, most programming courses start off with what's called a Hello World program
or a program that prints out Hello World on the screen when you run it.
In Python, a program to print Hello World looks like this.
In order to actually run this code, we need to click the Save & Run button.
When we do that, we'll see an output window show up to the right here.
Now to break down what all of this shows,
we have our Python source code on the left.
And we have our output on the right.
Now, when we click Save & Run what happens is that there's a hidden Python
interpreter.
And it looks at what's in our source code, and
prints out whatever any relevant output is.
And remember that this source code is a set of instructions.
In this case, the source code is an instruction to print out
whatever is in quotation marks here, in this case, Hello World.
If we wanted to change the set of instructions, so for
example, I want to change it to print out, Hello Michigan.
Then I need to put it through the interpreter by clicking Save & Run again,
and now when I do that,
you'll see that my output changed from Hello World, to Hello Michigan.
If I want to change it back, Then I need to click Save & Run.
Now, the Python interpreter typically tries to run all of the source
code that we put in this window.
But it can sometimes be helpful to leave natural language notes or explanations for
ourselves or for other programmers who are looking at the source code.
Now the Python interpreter typically tries to run
all of the source code that we put into our source code window, but
sometimes it can be helpful to leave natural language notes or explanations for
ourselves or for other programmers looking at the source code.
In order to do that, we have what are called comments.
In Python, we write a comment by using the # symbol and
writing what we want after it.
When we write a comment, then Python ignores everything that comes after the #
symbol, meaning that we can write whatever we want here.
And on the next line, Python will start running the code again.
So if we wanted to write a comment, we would need to add a # symbol.
And what comments do are they tell the Python interpreter to ignore
these portions of the source code, and only to run what's not commented.
In our case, the only code that actually runs here is print Hello World.
Another thing that's worth noting is that the Python interpreter is not very
forgiving.
So recall that when you write source code, you're giving the computer a set of
instructions, and these instructions need to be unambiguous,
they need to follow the rules of the Python programming language.
One of the rules of Python is that when we have an open parenthesis,
then we are going to need to have a closed parenthesis.
So, let's suppose that I forget that rule and
I delete this closed parenthesis, and I try to run my program.
What happens is that I get what's called a syntax error.
A syntax error is Python saying that it doesn't understand the rules of what you
wrote, so it doesn't try to actually execute what's in the source code window.
In other words, a syntactic error or a syntax error is when
you are not following the rules of the of the Python programming language.
In this case, we are not following the rule that this open parenthesis
has to be followed by a closed parenthesis.
Throughout this course, we're going to run into syntax errors and
other kinds of errors, including run time errors and semantic errors.
So when we get a syntax error, as we will many times throughout this course,
we can fix it by editing the source code to obey the syntactic rules of
the Python programming language.
In this case, I'm going to add a close parenthesis
to match the opening parenthesis that starts out here.
Now when I click Save & Run again,
then you'll see that the syntax error disappears and my program runs again.
With that, you're already on your way to becoming a programmer.
With more practice, you'll better understand how the Python interpreter
works, and will be able to write larger, and more complex programs.
See you next time.